﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using TelnetRouterApplication.Model;
using TelnetRouterApplication.Control;
using TelnetRouterApplication.Utils;

namespace TelnetRouterApplication.View
{
    public partial class SettingsPanel : UserControl
    {
        private SettingsModel _settings;
        public SettingsModel Settings
        {
            get { return _settings; }
            set { _settings = value; }
        }

        private MainForm _main;
        public MainForm Main
        {
            get { return _main; }
            set { _main = value; }
        }

        public SettingsPanel(MainForm main)
        {
            Main = main;

            InitializeComponent();
            Settings = null;
            do
            {
                Settings = CommandHandler.GetInstance().GetSettings();
            } while (Settings == null || Settings.Hostname.Equals("") || Settings.Version.Equals(""));
            ConfigutarionHandler.GetInstance().RouterSettings = Settings;
            setSettings();
        }

        public SettingsPanel() : this(null) { }

        private void setSettings()
        {
            if (Settings.IsRouter)
            {
                deviceBox.Text = "Router";
            }
            else
            {
                deviceBox.Text = "Switch";
            }
            routerNameBox.Text = Settings.Hostname;
            passwordBox.Text = Settings.Password;
            enablePasswordBox.Text = Settings.EnablePassword;
            routerTimeBox.Text = Settings.Clock;
            routerTimeMaskedBox.Text = Settings.MaskedClock;
            versionBox.Text = Settings.Version;
        }

        private void updateButton_Click(object sender, EventArgs e)
        {
            //Verificar campo a campo, quais foram modificados.

            string routerName = routerNameBox.Text;
            if (!routerName.Equals(Settings.Hostname))
            {
                //Chamar o método para mudar o nome do host
                CommandHandler.GetInstance().ChangeRouterName(routerName);
            }

            string password = passwordBox.Text;
            if (!password.Equals(Settings.Password))
            {
                //Chamar o método para mudar o password do vty
                CommandHandler.GetInstance().ChangeVTYPassword(password);
            }

            string enablePassword = enablePasswordBox.Text;
            if (!enablePassword.Equals(Settings.EnablePassword))
            {
                //Chamar o método para mudar o password do enable
                CommandHandler.GetInstance().ChangeRouterEnablePassword(enablePassword);
            }

            string maskedClock = routerTimeMaskedBox.Text;
            if (!maskedClock.Equals(Settings.MaskedClock))
            {
                //Chamar o método para mudar o clock do router
                CommandHandler.GetInstance().ChangeRouterClockTime(maskedClock);
            }

            //Não é possível mudar a versão do router

            //Chama o botão de refresh
            refreshBtn_Click(this, null);
        }

        private void refreshBtn_Click(object sender, EventArgs e)
        {
            Settings = CommandHandler.GetInstance().GetSettings();
            setSettings();

            MessageBox.Show("Refresh completed!");
        }

        private void exportBtn_Click(object sender, EventArgs e)
        {
            //Achei desnecessário checar se o settings é nulo

            string log = "";
            log += "Router Name@" + Settings.Hostname + "\r\n";
            log += "Password@" + Settings.Password + "\r\n";
            log += "Enable Password@" + Settings.EnablePassword + "\r\n";
            log += "Router Time@" + Settings.MaskedClock + "\r\n";
            log += "Version@" + Settings.Version + "\r\n";

            string path = IOUtils.SaveFileDialogShow(Main, "Text Files|*.txt");
            IOUtils.CreateTxtFile(path, log);
            MessageBox.Show("Log exported!");
        }

        private void importBtn_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Import settings will replace the actual settings for the imported ones. Continue?", "Import", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                string path = IOUtils.OpenFileDialogShow(Main);
                string[] importedSettings = IOUtils.ReadTxtFile(path);

                //Na ordem é para ser
                //0- router name
                //1- password
                //2- enable password
                //3- router time
                //4-version (imutável)

                //Precisa fazer toda a verificação

                string[] parameters = importedSettings[0].Split('@');
                if (parameters.Length > 0)
                {
                    routerNameBox.Text = parameters[1];
                }
                parameters = importedSettings[1].Split('@');
                if (parameters.Length > 0)
                {
                    passwordBox.Text = parameters[1];
                }
                else
                {
                    passwordBox.Text = "";
                }
                parameters = importedSettings[2].Split('@');
                if (parameters.Length > 0)
                {
                    enablePasswordBox.Text = parameters[1];
                }
                else
                {
                    enablePasswordBox.Text = "";
                }
                parameters = importedSettings[3].Split('@');
                if (parameters.Length > 0)
                {
                    routerTimeMaskedBox.Text = parameters[1];
                }

                updateButton_Click(sender, null);
            }
        }
    }
}
